<template>
  <div class="">
    <div class="search">
        <div class="input">
            <p><label>商品名称：</label><el-input placeholder="请输入内容" v-model="searchData.goodsName"></el-input></p>
            <p><label>商品状态：</label>
              <el-select v-model="searchData.goodsStatus"   placeholder="请选择">
                <el-option value="">请选择</el-option>
                  <el-option
                    v-for="item in Online"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
            </p>
            <p><el-button type="primary" class="se" style="margin-left:10px;" @click="search">搜索</el-button></p>
        </div>
    </div>
    <div class="add">
      <el-button type="primary" @click="add">新增商品</el-button>
      <el-button type="danger" @click="del">批量删除</el-button>
    </div>
    <tableVue :list="list" :state="true" v-loading="pageData.loading" :total="total"  @changeData="selectionVal" @sizeChange="handSize">
        <slot>
          <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  v-if="scope.row.goodsStatus == '待上架'"
                  size="mini"
                  type="success"
                  @click="putaway(scope.row)">上架
                </el-button>
                <el-button
                  v-if="scope.row.goodsStatus == '已上架'"
                  size="mini"
                  type="warning"
                  @click="out(scope.row)">下架
                </el-button>
                <el-button
                  size="mini"
                  @click="compile(scope.row)">编辑
                </el-button>
                <el-button
                  size="mini"
                  type="danger"
                  class="del"
                  @click="handleEdit(scope.row,2)">删除</el-button>
              </template>
          </el-table-column>
        </slot>
    </tableVue>
  </div>
</template>

<script>

  import Data from './productAdminData.js'
  import dialogVue from '@/components/dialog.vue'
  import tableVue from '@/components/alltable.vue'
  import { Base64 } from 'js-base64';
  import checkPermission from "@/utils/permission"; // 权限判断函数
  import {
    commodityList
  } from "@/api/shoppingCentre/productCentre";
  import {
    goodsPutaway,
    delgoods
  } from "@/api/shoppingCentre/shoppingCentre";

  export default {
    components : {
      tableVue,
      dialogVue
    },
    data () {
      return JSON.parse(JSON.stringify(Data))
    },
    mounted () {
      var data = {};
      this.getproductList(data)
    },
    methods : {
      handSize (val) {
        console.log(this.searchData);
        this.getproductList(this.searchData,val)

      },
      getproductList (data,val) {//商品列表
        data['current'] = val;
        data['size'] = '10'
        this.pageData.loading = true;
        commodityList(data).then(res=>{
          console.log(res);
          if (res.data.code == 'OK') {
            this.pageData.loading = false;
            this.total = res.data.data.total;
            this.list.tbody = res.data.data.records.map(item=>{
               item.gmtCreate = this.switchTime(item.gmtCreate)
               item.gmtUpdate = this.switchTime(item.gmtUpdate)
               switch (item.goodsStatus) {
                 case '1' : item.goodsStatus = '待上架'
                 break;
                 case '2':  item.goodsStatus = '已上架'
                 break;
                 case '3':  item.goodsStatus = '已预订'
                 break;
                 case '4':  item.goodsStatus = '已出售'
                 break;
                 default:
               }
               return item;
            });
             this.total = res.data.data.total;
          } else {
            this.$message.error(res.data.message);
          }
        })
      },
      add () {
        this.$store.state.category.categoryId = ''
        this.$store.state.category.goodsId = ''
        this.$router.push({
          name : 'basics',
        })
      },
      compile (item) {
        this.$store.state.category.idx = '2'
        this.$router.push({
          name : 'basics',
          query : {
            id :  item.goodsId
          }
        })
      },
      changeData (val) {
        this.dialog = val;
      },
      selectionVal (data) {
          this.pageData.goodsId = data;
      },
      handleEdit (item) {
        this.del()
      },
      del () {
        if (this.pageData.goodsId.length > 0) {
          let id = []
          this.pageData.goodsId.map(item=>{
             id.push(item.goodsId)
          })
          this.delMessage(()=>{
            delgoods(id).then(res=>{
              if (res.data.code == 'OK') {
                  this.$message.success('删除成功');
                  var data = {}
                  this.getproductList(data);
              } else {
                this.$message.error(res.data.message);
              }
            })
          })
        } else {
            this.$message.error('请选择要删掉的商品');
        }
      },
      search () {
          this.getproductList(this.searchData,'')
      },
      putaway (item) {//下架
        this.$confirm('此操作将永久上架该商品, 是否继续?', '提示', {
           confirmButtonText: '确定',
           cancelButtonText: '取消',
           type: 'warning'
         }).then(() => {
            var data = {
              goodsId : item.goodsId,
              onlineType : '1'
            }
           goodsPutaway(data).then(res=>{
             if (res.data.code == 'OK') {
                this.$message.success('该商品下架成功');
                var data = {};
                this.getproductList(data)
             } else {
                 this.$message.error(res.data.message);
             }
           })
         }).catch(() => {
           this.$message({
             type: 'info',
             message: '已取消删除'
           });
         });
      },
      out (item) {//上架
        this.$confirm('此操作将永久下架该商品, 是否继续?', '提示', {
           confirmButtonText: '确定',
           cancelButtonText: '取消',
           type: 'warning'
         }).then(() => {
           var data = {
             goodsId : item.goodsId,
             onlineType : '2'
           }
           goodsPutaway(data).then(res=>{
             if (res.data.code == 'OK') {
                this.$message.success('该商品上架成功');
                var data = {};
                this.getproductList(data)
             } else {
                 this.$message.error(res.data.message);
             }
           })
         }).catch(() => {
           this.$message({
             type: 'info',
             message: '已取消删除'
           });
         });
      },
    }
  }
</script>

<style scoped>
.search {
  width: 100%;
  background-color: #fff;
  padding:20px;
  box-sizing: border-box;
  margin-top: 10px;
}
.input {
  /* width: 300px; */
  display: flex;
  flex-wrap: wrap;
}
.input p {
  margin-top: 10px;
}
label {
  line-height: 40px;
  color: #333;
  margin: 0 10px;
}
.el-input {
  width:200px;
  height:20px;
}
.add {
  margin-top: 10px;
  padding-left: 14px;
}
.se {
  margin-left: 20px;
}
.confirm {
  position: absolute;
  bottom: -1px;
  right: 100px;
}
</style>
